package blue.squares;

import android.graphics.Point;

import java.util.ArrayList;

/**
 *
 * @author James Bruce
 */
public class RandomStrategy extends Strategy {
    // ------------------------------------------------------------------------
    // Fields.
    // ------------------------------------------------------------------------

    final ArrayList<Point> _sequence;

    // ------------------------------------------------------------------------
    // Implementation.
    // ------------------------------------------------------------------------

    public RandomStrategy(Game game, Player player) {
        super(game, player);
        _sequence = generateSequence(_game.getBoardSize());
    }

    private ArrayList<Point> generateSequence(int boardSize) {
        ArrayList<Point> s = new ArrayList();
        for(int x = 0; x < boardSize; x++) {
            for(int y = 0; y < boardSize; y++) {
                s.add(new Point(x,y));
            }
        }
        Util.permuteRandomly(s);
        return s;
    }

    public Point chooseMove() {
        // Find next move in our sequence which is empty.
        while(!_sequence.isEmpty()){
            Point location = _sequence.remove(0);
            if(_game.isFree(location.x, location.y))
                return location;
        }
        return null;
    }
}
